<?php
defined('OA_LIB_PATH') || define('OA_LIB_PATH', '/opt/op/OALib');

require_once OA_LIB_PATH . '/util/config/Config.php';
require_once OA_LIB_PATH . '/inner/stat/OaGearmanClient.php';
require_once OA_LIB_PATH . '/inner/stat/OaGearmanAgent.php';

class OaStatClient extends OaGearmanClient
{
    private static $_instance;
    
    private $_useAgent = true;
    private $_gearmanAgent;
    
    public function __construct($logger = null)
    {
        $jobServers = Oa_Util_Config::getConfig('job_servers');
        parent::__construct($jobServers, $logger);
        $this->_gearmanAgent = new OaGearmanAgent($logger);
    }
    
    public function setUseAgent($useAgent)
    {
        $this->_useAgent = $useAgent;
    }
    
    public function getUseAgent()
    {
        return $this->_useAgent;
    }
    
    /**
     * @param string $task
     * @param string $workload
     * @return boolean
     */
    public function send($task, $workload)
    {
        if ($this->_useAgent) {
            return $this->_gearmanAgent->send($task, $workload);
        } else {
            $this->setTimeout(1);
            return $this->doTask($task, $workload, 0, OaGearmanClient::TASK_PRIORITY_LOW, 
                true);
        }
    }
    
    /**
     * 记录日志
     * @param string $type 日志类型
     * @param string $message 日志内容
     * @return boolean
     */
    public function log($type, $message)
    {
        if (empty($type)) {
        	$this->err("type is empty");
            return false;
        }
        return $this->send('log', json_encode(array(
            'type' => $type,
            'message' => $message
        )));
    }
}
